const App = Vue.createApp({
    data() {
        return {
            UserData: {
                LoginAnnouncement: '', TradeMarkPublication: '', login: 1, //账号密码登录或注册
                logo: '', phone: -1, //是否开启了手机号登录
                qqlogin: '', //QQ互联跳转地址
                qqtype: '', //是否启用QQ互联登录
                title: '',
                CaptchaType: 1, //验证码类型，8=极验，其他=图片
                GtCaptchaId: '', //极验验证ID
                UserAgreement: '', //用户协议
                PrivacyPolicy: '',//隐私政策
                LegalNotice: '', //法律声明
                ProtocolSwitch: false, //是否开启协议
                AccountRetrieval: -1, //是否可以找回账号
                RegisterPhone: -1,
            },
            phone: '', //手机号
            code: '', //验证码
            phoneType: 1, //手机号登录状态
            user: '', pass: '', qq: '', LoginType: 1, //显示模式，1账号密码，2手机
            PrivacyState: false, //协议勾选状态
            StateType: 1, //显示状态，1=登录，2=注册，3=手机号登录
        }
    }, mounted() {
        this.UserDataGet();
        this.LoginStatus();
    }, methods: {
        LoginStatus() {
            //账号登录检测，5秒一次，如果处于登录状态，自动刷新页面！
            $.ajax({
                url: ROOT_DIR_S + '/user/ajax.php?act=LoginStatus',
                type: 'POST',
                dataType: 'json',
                success: (res) => {
                    if (res.code === 1) {
                        TopLogin(HREF);
                    } else {
                        setTimeout(function () {
                            App.LoginStatus();
                        }, 5000);
                    }
                }
            })
        },
        //找回密码
        FindPassword() {
            var pageWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
            let area = ['450px', '500px'];
            if (pageWidth < 500) {
                area = ['95%', '95%'];
            }
            layer.open({
                title: '找回密码', type: 2, shadeClose: true, maxmin: true, //开启最大化最小化按钮
                area: area, content: HREF + ROOT_DIR_S + '/?mod=Find&gid=' + GID, end: function () {
                    App.UserDataGet(2);
                }
            });
        }, checkTelephone(telephone) {
            const reg = /^1[3-9][0-9]{9}$/;
            return reg.test(telephone);
        }, //极验验证集成
        CaptchaGet(name_id) {
            if (this.UserData.ProtocolSwitch && !this.PrivacyState) {
                layer.alert('请先同意协议', {icon: 2});
                return false;
            }
            let _this = this;
            switch (name_id) {
                case 'Login1'://账号密码登录
                    if (App.user === '' || App.pass === '') {
                        layer.alert('请输入账号密码', {icon: 2});
                        return false;
                    }
                    break;
                case 'Login2'://手机登录
                    if (App.phone === '' || !App.checkTelephone(App.phone)) {
                        layer.alert('请输入正确的手机号', {icon: 2});
                        return false;
                    }
                    break;
                case 'Register': //新用户注册
                    if (App.user === '' || App.pass === '' || App.qq === '') {
                        layer.alert('请输入账号密码和QQ', {icon: 2});
                        return false;
                    }
                    //强制绑定手机号，无需本地验证，直接验证短信即可！
                    if (App.UserData.RegisterPhone === 1 && App.UserData.phone === 1) {
                        _this.loginRegister();
                        return false;
                    }
                    break;
                case 'RegisterSms': //发送短信验证码
                    if (App.phone === '' || !App.checkTelephone(App.phone)) {
                        layer.alert('请输入正确的手机号', {icon: 2});
                        return false;
                    }
                    break;
            }
            //初始化验证
            let captchaBox = document.getElementById(name_id);
            initGeetest4({
                captchaId: _this.UserData.GtCaptchaId, product: 'bind', language: 'zho', userInfo: name_id, //验证类型,必须提交和验证时一致
            }, function (captchaObj) {
                captchaObj.reset();
                captchaObj.showBox();
                captchaObj.appendTo(captchaBox).onSuccess(function (e) {
                    var result = captchaObj.getValidate();
                    if (!result) {
                        alert('请先完成验证！');
                        window.location.reload();
                        return false;
                    }
                    result.name = name_id; //判断验证类型
                    switch (name_id) {
                        case 'Login1': //账号密码登录
                            _this.loginRequest(result);
                            break;
                        case 'Login2': //手机登录
                            _this.SendVerificationCode(result);
                            break;
                        case 'Register': //新用户注册
                            _this.loginRegister(result);
                            break;
                        case 'RegisterSms': //发送注册短信验证
                            _this.SendRegisterCode(result);
                            break;
                        default:
                            //默认
                            layer.alert('验证类型错误', {icon: 2});
                            break;
                    }
                })
            });
        }, Inform(type = 1) {
            if (type === 1) {
                if (this.UserData.LoginAnnouncement === '') return;
            } else {
                if (this.UserData.TradeMarkPublication === '') return;
            }
            swal({
                title: type === 1 ? '登录须知' : '注册须知',
                text: type === 1 ? this.UserData.LoginAnnouncement : this.UserData.TradeMarkPublication,
            });
            $(".swal-text").html(type === 1 ? this.UserData.LoginAnnouncement : this.UserData.TradeMarkPublication);
        }, //登录请求
        loginRequest(gt_data = {}) {
            if (this.UserData.ProtocolSwitch && !this.PrivacyState) {
                layer.alert('请先同意协议', {icon: 2});
                return false;
            }
            let _this = this;
            let data = {
                user: App.user, pass: App.pass, GT: gt_data,
            }
            if (data.user === '' || data.pass === '') {
                layer.alert('请输入账号密码', {icon: 2});
                return false;
            }
            if (_this.UserData.CaptchaType !== 8) {
                let image = ROOT_DIR_S + '/user/ajax.php?act=VerificationCode&n=Login_vc&t=';
                layer.open({
                    type: 1,
                    title: '请输入验证码',
                    content: App.ContentHtml(image),
                    btn: ['确定', '取消'],
                    yes: function () {
                        var code = $("#captcha").val();
                        if (code === '') {
                            layer.alert('请输入验证码', {
                                icon: 2
                            });
                            return false;
                        }
                        layer.closeAll();
                        data.vercode = code;
                        data.GT = null;
                        App.loginRequestAjax(data);
                    },
                    success: function () {
                        $("#Image").click(function () {
                            $("#Image").attr('src', image + Math.random());
                        })
                    }
                })
            } else {
                //直接提交,极验验证
                App.loginRequestAjax(data);
            }
        }, loginRequestAjax(data) {
            layer.msg('正在登录中,请稍后', {icon: 16, time: 9999999});
            $.ajax({
                url: ROOT_DIR_S + '/user/ajax.php?act=login_account',
                data: data,
                type: 'POST',
                dataType: 'json',
                success: (res) => {
                    if (res.code >= 1) {
                        layer.alert(res.msg, {
                            icon: 1, end: function () {
                                TopLogin(HREF);
                            }
                        });
                    } else {
                        layer.alert(res.msg, {icon: 2});
                    }
                }
            });
        }, //用户注册
        loginRegister(gt_data = {}) {
            if (this.UserData.ProtocolSwitch && !this.PrivacyState) {
                layer.alert('请先同意协议', {icon: 2});
                return false;
            }
            let _this = this;
            let data = {
                username: App.user, password: App.pass, qq: App.qq, GT: gt_data,
            };
            if (data.username === '' || data.password === '' || data.qq === '') {
                layer.alert('请输入账号密码和QQ', {icon: 2});
                return false;
            }
            //强制绑定手机号
            if (App.UserData.RegisterPhone === 1 && App.UserData.phone === 1) {
                if (App.phone === '' || !App.checkTelephone(App.phone)) {
                    layer.alert('请输入正确的手机号', {icon: 2});
                    return false;
                }
                if (App.code === '') {
                    layer.alert('请填写短信验证码！', {icon: 2});
                    return false;
                }
                data.code = _this.code;
                _this.loginRegisterAjax(data);
                return true;
            }
            if (_this.UserData.CaptchaType !== 8) {
                let image = ROOT_DIR_S + '/user/ajax.php?act=VerificationCode&n=Login_res&t=';
                layer.open({
                    type: 1,
                    title: '请输入验证码',
                    content: App.ContentHtml(image),
                    btn: ['确定', '取消'],
                    yes: function () {
                        var code = $("#captcha").val();
                        if (code === '') {
                            layer.alert('请输入验证码', {
                                icon: 2
                            });
                            return false;
                        }
                        layer.closeAll();
                        data.vercode = code;
                        data.GT = null;
                        App.loginRegisterAjax(data);
                    },
                    success: function () {
                        $("#Image").click(function () {
                            $("#Image").attr('src', image + Math.random());
                        })
                    }
                })
            } else {
                _this.loginRegisterAjax(data);
            }
        }, loginRegisterAjax(data) {
            layer.msg('正在注册中,请稍后', {icon: 16, time: 9999999});
            $.ajax({
                url: ROOT_DIR_S + '/user/ajax.php?act=login_register',
                data: data,
                type: 'POST',
                dataType: 'json',
                success: (res) => {
                    if (res.code >= 1) {
                        layer.alert(res.msg, {
                            icon: 1, end: function () {
                                TopLogin(HREF);
                            }
                        });
                    } else {
                        layer.alert(res.msg, {icon: 2});
                    }
                }
            });
        }, //验证短信是否正确
        CodeLogin() {
            if (this.UserData.ProtocolSwitch && !this.PrivacyState) {
                layer.alert('请先同意协议', {icon: 2});
                return false;
            }
            layer.msg('正在登录中,请稍后', {icon: 16, time: 9999999});
            //验证短信是否正确
            $.ajax({
                url: ROOT_DIR_S + '/user/ajax.php?act=Send_verification_login', data: {
                    code: App.code,
                }, type: 'POST', dataType: 'json', success: (res) => {
                    if (res.code >= 1) {
                        layer.alert(res.msg, {
                            icon: 1, end: function () {
                                TopLogin(HREF);
                            }
                        });
                    } else {
                        layer.alert(res.msg, {icon: 2});
                    }
                }
            });
        },
        SendRegisterCode(gt_data = {}) {
            if (this.UserData.ProtocolSwitch && !this.PrivacyState) {
                layer.alert('请先同意协议', {icon: 2});
                return false;
            }
            let _this = this;
            let data = {
                phone: App.phone, GT: gt_data
            }
            if (App.phone === '' || !App.checkTelephone(App.phone)) {
                layer.alert('请输入正确的手机号', {icon: 2});
                return false;
            }
            if (_this.UserData.CaptchaType !== 8) {
                let image = ROOT_DIR_S + '/user/ajax.php?act=VerificationCode&n=RegisterSms&t=';
                layer.open({
                    type: 1,
                    title: '请输入验证码',
                    content: App.ContentHtml(image),
                    btn: ['确定', '取消'],
                    yes: function () {
                        var code = $("#captcha").val();
                        if (code === '') {
                            layer.alert('请输入验证码', {
                                icon: 2
                            });
                            return false;
                        }
                        layer.closeAll();
                        data.code = code;
                        data.GT = null;
                        _this.SendRegisterCodeAjax(data);
                    },
                    success: function () {
                        $("#Image").click(function () {
                            $("#Image").attr('src', image + Math.random());
                        })
                    }
                })
            } else {
                _this.SendRegisterCodeAjax(data);
            }
        },
        SendRegisterCodeAjax(data = {}) {
            layer.msg('正在发送中,请稍后', {icon: 16, time: 9999999});
            console.log(data);
            $.ajax({
                url: ROOT_DIR_S + '/user/ajax.php?act=RegisterSms',
                data: data,
                type: 'POST',
                dataType: 'json',
                success: (res) => {
                    if (res.code >= 1) {
                        //验证码发送成功
                        App.code = '';
                        layer.alert(res.msg, {icon: 1});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }
            });
        },
        //发送验证码
        SendVerificationCode(gt_data = {}) {
            if (this.UserData.ProtocolSwitch && !this.PrivacyState) {
                layer.alert('请先同意协议', {icon: 2});
                return false;
            }
            let _this = this;
            let data = {
                mobile: App.phone, GT: gt_data
            }
            if (App.phone === '' || !App.checkTelephone(App.phone)) {
                layer.alert('请输入正确的手机号', {icon: 2});
                return false;
            }
            if (_this.UserData.CaptchaType !== 8) {
                let image = ROOT_DIR_S + '/user/ajax.php?act=VerificationCode&n=Login_sms_vis&t=';
                layer.open({
                    type: 1,
                    title: '请输入验证码',
                    content: App.ContentHtml(image),
                    btn: ['确定', '取消'],
                    yes: function () {
                        var code = $("#captcha").val();
                        if (code === '') {
                            layer.alert('请输入验证码', {
                                icon: 2
                            });
                            return false;
                        }
                        layer.closeAll();
                        data.code = code;
                        data.GT = null;
                        App.SendVerificationCodeAjax(data);
                    },
                    success: function () {
                        $("#Image").click(function () {
                            $("#Image").attr('src', image + Math.random());
                        })
                    }
                })
            } else {
                _this.SendVerificationCodeAjax(data);
            }
        }, SendVerificationCodeAjax(data) {
            let _this = this;
            layer.msg('正在发送中,请稍后', {icon: 16, time: 9999999});
            $.ajax({
                url: ROOT_DIR_S + '/user/ajax.php?act=Send_verification_code_login',
                data: data,
                type: 'POST',
                dataType: 'json',
                success: (res) => {
                    if (res.code >= 1) {
                        //验证码发送成功
                        _this.phoneType = 2; //发送成功
                        _this.code = '';
                        layer.alert(res.msg, {icon: 1});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }
            });
        }, //获取登录参数
        UserDataGet(type = 1) {
            layer.msg('数据载入中,请稍后', {icon: 16, time: 9999999});
            let _this = this;
            $.ajax({
                url: ROOT_DIR_S + '/main.php?act=UserData', type: 'POST', dataType: 'json', success: (res) => {
                    $("#App").fadeIn(1000);
                    $("#AppHide").fadeOut(500);
                    layer.closeAll();
                    if (res.code >= 1) {
                        TopLogin(HREF);
                    } else {
                        //未登录
                        _this.UserData = res.data;
                        if (type === 1) {
                            _this.Inform(1);
                            //判断是否启用了极验验证
                            if (_this.UserData.CaptchaType === 8) {
                                _this.addScript(ROOT_DIR_S + '/assets/js/gt4.js');
                            }
                        }
                    }
                }
            });
        }, addScript(url) {
            var script = document.createElement('script');
            script.setAttribute('type', 'text/javascript');
            script.setAttribute('src', url);
            document.getElementsByTagName('head')[0].appendChild(script);
        }, ContentHtml(image) { //验证码弹窗
            return `
            <style>.layui-layer-page .layui-layer-content{overflow:hidden;}</style>
            <div class="layui-form" style="padding:1em 1em 0 1em;max-width:320px;">
                <div class="layui-form-item">
                    <div class="layui-row" style="width: 94%;margin:auto;">
                        <div class="layui-col-xs12">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-vercode"></i>
                                </div>
                                <input type="text" id="captcha" value="" style="min-width:auto;" placeholder="图片验证码"
                                       lay-reqtext="请填写验证码" class="layui-input" >
                            </div>
                        </div>
                        <div class="layui-col-xs12">
                            <div  style="text-align: center;margin-top: 1em;cursor: pointer;">
                                <img src="` + image + `" style="width: 150px;" id="Image">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        `;
        },
    }
}).mount('#App');

var TpType = 1; //1=max，2=min
try {
    const singUpBtn = document.querySelector('#sign-up-btn')
    const singInBtn = document.querySelector('#sign-in-btn')
    const container = document.querySelector('.container')

    singUpBtn.addEventListener('click', () => {
        container.classList.add('sign-up-mode')
    })
    singInBtn.addEventListener('click', () => {
        container.classList.remove('sign-up-mode')
    })
    console.log('当前Max模板');
    TpType = 1;
} catch (e) {
    console.log('当前Min模板');
    TpType = 2;
}

function TopLogin(url) {
    url += ROOT_DIR_S;
    if (window.self !== window.top) {
        if (GID >= 1) {
            top.location.href = url + '/?mod=route&p=Goods&gid=' + GID;
        } else {
            top.location.reload();
        }
    } else {
        if (GID >= 1) {
            window.location.href = url + '/?mod=route&p=Goods&gid=' + GID;
        } else {
            window.location.href = url;
        }
    }
}

function handleResizes() {
    var screenHeight = window.innerHeight;
    var screenWidth = window.innerWidth;
    let SmallTail = '';
    if (GID >= 1) {
        SmallTail = '&gid=' + GID;
    }
    if (screenHeight < 568 || screenWidth < 320) {
        if (TpType === 1) {
            window.location.href = ROOT_DIR_S + '/?mod=login_min' + SmallTail;
        }
    } else {
        if (TpType === 2) {
            window.location.href = ROOT_DIR_S + '/?mod=login' + SmallTail;
        }
    }
}

handleResizes();
if (window.self !== window.top) {
    console.log('当前为iframe模式');
}